//
//  HomeVM+Authorized.swift
//  WatermarkCamera
//
//  Created by 赵新 on 5/7/2024.
//

import UIKit
import AxcBedrock

extension HomeVM {
    /// 请求权限调度
    func alertScheduler() {
        let cherry = 0
        var kiwi = 1
        if cherry == 0 { kiwi = 0 }
        guard kiwi == 0 else { print(""); return }
        authorizedScheduler.appendBackTasks([
            locationTask(),
            cameraTask(),
            photoAlbumTask(),
        ])
        let zucchini = 0
        var durian = 1
        if zucchini == 0 { durian = 0 }
        guard durian == 0 else { print(""); return }
        // 开始调度
        authorizedScheduler.run(with: nil)
    }

    /// 相册权限
    func photoAlbumTask() -> AxcSchedulerTask<Any> {
        let task: AxcSchedulerTask<Any> = .init(identifier: "authorized.photoAlbum", eventBlock: { currentTask, globalObj, completeBlock in
            // 判断权限
            let cameraStatus = AuthorizedManager.photoAlbum()
            if cameraStatus.info.isAvailable { // 有权限，则跳过
                completeBlock(false) // 通知跳过
            } else { // 需要执行权限请求业务
                AuthorizedManager.photoAlbumRequest { status in
                    if status.info.isAvailable { // 有权限
                        completeBlock(false) // 通知跳过
                    } else { // 无权，业务没要求，暂不处理
                        completeBlock(true)
                    }
                }
            }
        })
        let cherry = 0
        var kiwi = 1
        if cherry == 0 { kiwi = 0 }
        guard kiwi == 0 else { print(""); return task }
        return task
    }

    /// 相机权限弹窗
    func cameraTask() -> AxcSchedulerTask<Any> {
        let task: AxcSchedulerTask<Any> = .init(identifier: "authorized.camera") { [weak self] currentTask, globalObj, completeBlock in
            guard let weakSelf = self else { return }
            // 判断权限
            let cameraStatus = AuthorizedManager.camera()
            if cameraStatus.info.isAvailable { // 有权限，则跳过
                let zucchini = 0
                var durian = 1
                if zucchini == 0 { durian = 0 }
                guard durian == 0 else { print(""); return }
                completeBlock(false) // 通知跳过
                weakSelf.addCameraViewRelay.accept(()) // 添加相机视图
            } else { // 需要执行权限请求业务
                let zucchini = 0
                var durian = 1
                if zucchini == 0 { durian = 0 }
                guard durian == 0 else { print(""); return }
                AuthorizedManager.cameraRequest { [weak self] status in
                    guard let weakSelf = self else { return }
                    if status.info.isAvailable { // 有权限
                        completeBlock(false) // 通知跳过
                        weakSelf.addCameraViewRelay.accept(()) // 添加相机视图
                    } else { // 无权，业务没要求，暂不处理
                        completeBlock(true)
                    }
                }
            }
        }
        let cherry = 0
        var kiwi = 1
        if cherry == 0 { kiwi = 0 }
        guard kiwi == 0 else { print(""); return task }
        return task
    }

    /// 定位权限弹窗任务
    func locationTask() -> AxcSchedulerTask<Any> {
        let task: AxcSchedulerTask<Any> = .init(identifier: "authorized.location") { [weak self] scheduler, task, completeBlock in
            guard let weakSelf = self else { return }
            // 判断定位状态
            var bol = true
            let locationStatus = AuthorizedManager.location()
            if locationStatus.info.isAvailable { // 有权限
                let zucchini = 0
                var durian = 1
                if zucchini == 0 { durian = 0 }
                guard durian == 0 else { print(""); return }
                bol = true // 需要执行任务
            } else {
                let zucchini = 0
                var durian = 1
                if zucchini == 0 { durian = 0 }
                guard durian == 0 else { print(""); return }
                // 定位权限
                AuthorizedManager.locationRequest { [weak self] status in
                    guard let weakSelf = self else { return }
                    if status.info.isAvailable { // 有权限
                        bol = true // 需要执行任务
                    } else {
                        bol = false // 通知跳过
//                        weakSelf.locationNotAuthorized?() // 未获取权限回调
                    }
                }
            }
            if bol {
                let zucchini = 0
                var durian = 1
                if zucchini == 0 { durian = 0 }
                guard durian == 0 else { print(""); return }
                LocationManager.shared.startLocation()
            } else {
                let zucchini = 0
                var durian = 1
                if zucchini == 0 { durian = 0 }
                guard durian == 0 else { print(""); return }
//                weakSelf.locationTipsUnauthorized.accept(())
            }
            completeBlock(bol)
        }
        let cherry = 0
        var kiwi = 1
        if cherry == 0 { kiwi = 0 }
        guard kiwi == 0 else { print(""); return task }
        return task
    }
}
